package com.kyl.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kyl.base.PageResponse;
import com.kyl.dto.NursingProjectDto;
import com.kyl.entity.NursingProject;
import com.kyl.mapper.NursingProjectMapper;
import com.kyl.service.NursingProjectService;
import com.kyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author WanJl
 * @version 1.0
 * @title NursingProjectServiceImpl
 * @description 护理项目Service实现类
 * @create 2025/1/10
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 根据条件分页查询护理项目
     *
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize, "order_no ASC,create_time DESC");
        List<NursingProjectVo> nursingProjects = nursingProjectMapper.selectByPage(name, status, pageNum, pageSize);
        Page<NursingProjectVo> pages = (Page<NursingProjectVo>) nursingProjects;
        return PageResponse.of(pages, NursingProjectVo.class);
    }

    @Override
    public List<NursingProjectVo> getList() {
        List<NursingProject> list = nursingProjectMapper.getList();
        List<NursingProjectVo> nursingProjectVoList= null;
        if (list != null && list.size() > 0) {
            nursingProjectVoList=list.stream().map(nursingProject -> {
                NursingProjectVo bean = BeanUtil.toBean(nursingProject, NursingProjectVo.class);
                return bean;
            }).collect(java.util.stream.Collectors.toList());
        }
        return nursingProjectVoList;
    }

    /**
     * 新增护理项目
     *
     * @param nursingProjectDTO 护理项目数据传输对象
     */
    @Override
    public void add(NursingProjectDto nursingProjectDTO) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDTO, nursingProject);
        nursingProjectMapper.insert(nursingProject);
    }

    /**
     * 根据ID查询护理项目
     *
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getById(Long id) {

        return nursingProjectMapper.selectById(id);
    }

    @Override
    public void update(NursingProjectDto nursingProjectDTO) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDTO, nursingProject);
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    public void enableOrDisable(Long id, Integer status) {
        nursingProjectMapper.updateStatus(id, status);
    }

    @Override
    public void deleteById(Long id) {
        nursingProjectMapper.deleteById(id);
    }
}